package cn.nfwx.tv.manager;

import com.chan.net.DefaultObserver;
import com.chan.net.HttpCallback;
import com.chan.net.Result;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.List;

import cn.nfwx.tv.base.NFWXApplication;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.helper.LocationHelper;
import cn.nfwx.tv.helper.ThreadHelper;
import cn.nfwx.tv.model.Company;
import cn.nfwx.tv.model.NyqfBean;
import cn.nfwx.tv.model.TvChannel;
import cn.nfwx.tv.model.TvProgram;
import cn.nfwx.tv.okhttp.Http;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okio.ByteString;
import retrofit2.http.GET;
import retrofit2.http.Query;

public class NyqfManager {

    public static String getnyqfTitles(LifecycleTransformer<Result<NyqfBean>> lifecycleTransformer, HttpCallback httpCallback) {
        String request_tag = "getnyqfTitles" + System.currentTimeMillis();
        request_tag = ByteString.encodeUtf8(request_tag).md5().hex();
        NyqfApi api = Http.getInstance().getNyqfRetrofit(NyqfApi.class);
        Observable<Result<NyqfBean>> observable = api.getnyqfTitles();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(lifecycleTransformer)
                .subscribe(new DefaultObserver(request_tag, httpCallback));
        return request_tag;
    }

    /**
     * 南粤清风News信息
     *
     * @param nav
     * @param lifecycleTransformer
     * @param httpCallback
     * @return
     */
    public static String getnyqfNews(String nav, LifecycleTransformer<Result<NyqfBean>> lifecycleTransformer, HttpCallback httpCallback) {
        String request_tag = "getnyqfNews" + System.currentTimeMillis();
        request_tag = ByteString.encodeUtf8(request_tag).md5().hex();
        NyqfApi api = Http.getInstance().getNyqfRetrofit(NyqfApi.class);
        Observable<Result<NyqfBean>> observable = api.getnyqfNews(nav);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(lifecycleTransformer)
                .subscribe(new DefaultObserver(request_tag, httpCallback));
        return request_tag;
    }

    /**
     * 获取
     *
     * @return
     */
    public static String getChannelLives(LifecycleTransformer<Result<List<TvChannel>>> lifecycleTransformer, HttpCallback httpCallback) {
        String request_tag = "getChannelLives" + System.currentTimeMillis();
        request_tag = ByteString.encodeUtf8(request_tag).md5().hex();
        NyqfApi api = Http.getInstance().getLiveRetrofit(NyqfApi.class);
        Observable<Result<List<TvChannel>>> observable = api.channelLives(LocationHelper.getInstance().getRegion(), AccountHelper.getInstance().getUid(NFWXApplication.getInstance()));
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(lifecycleTransformer)
                .subscribe(new DefaultObserver(request_tag, httpCallback));
        return request_tag;
    }

    /**
     * 热门视频
     */
    public static String getHotPrograms(int start, int size, LifecycleTransformer<Result<List<TvProgram>>> lifecycleTransformer, HttpCallback httpCallback) {
        String request_tag = "getHotPrograms" + System.currentTimeMillis();
        request_tag = ByteString.encodeUtf8(request_tag).md5().hex();
        NyqfApi api = Http.getInstance().getCommentRetrofit(NyqfApi.class);
        Observable<Result<List<TvProgram>>> observable = api.getHotPrograms(start, size);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(lifecycleTransformer)
                .subscribe(new DefaultObserver(request_tag, httpCallback));
        return request_tag;
    }

    private interface NyqfApi {
        @GET("api/nyqf")
        Observable<Result<NyqfBean>> getnyqfNews(@Query("nav") String nav);

        @GET("api/nyqf?load=0")
        Observable<Result<NyqfBean>> getnyqfTitles();

        @GET("api/querylive")
        Observable<Result<List<TvChannel>>> channelLives(@Query("region") String region, @Query("uid") int uid);

        @GET("api/hotvideos")
        Observable<Result<List<TvProgram>>> getHotPrograms(@Query("start") int start, @Query("size") int size);
    }
}
